# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields,osv
import facebook

class res_partner(osv.osv):
    
    def _fb_project_count(self, cr, uid, ids, field_name, arg, context=None):
        Task = self.pool['project.project']
        return {
            partner_id: Task.search_count(cr,uid, [('partner_id', '=', partner_id)], context=context)
            for partner_id in ids
        }
    _inherit = 'res.partner'
    _columns = {
        'fb_feference': fields.char('Facebook Reference',size=128),
        'access_token': fields.char('Access Token',size=None),
        'fb_project_ids': fields.one2many('project.project', 'partner_id', 'Projects'),
        'fb_project_count': fields.function(_fb_project_count, string='# Facebook Projects', type='integer'),
    }
    def get_facebook_pages(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        for cus in self.browse(cr, uid, ids, context=context):
                if cus.fb_feference != None:
                    fb_id = cus.fb_feference
                    access_token = cus.access_token
                    graph = facebook.GraphAPI(access_token)
                    user = graph.get_object(fb_id)
                    getojb = graph.get_connections(user["id"], "accounts")
                    accounts = getojb['data']
                    for pageline in accounts:
                        page_access_token = pageline['access_token']
                        page_category = pageline['category']
                        page_name = pageline['name']
                        page_id = pageline['id']
                        project_page = self.pool.get('project.project')
                        check_page_id = project_page.search(cr,uid,[('page_id','=',page_id)])
                        if len(check_page_id) == 0:
                            project_id = project_page.create(cr,uid,{'name':page_name,
                                                                     'page_id':page_id,
                                                                     'category':page_category,
                                                                     'access_token':page_access_token,
                                                                     'partner_id': cus.id,})
                            check_page_id = []
                            check_page_id.append(project_id)
                        else:
                            project_page.write(cr, uid, check_page_id, {'access_token': page_access_token}, context)
                        
                        page_bro = project_page.browse(cr,uid,check_page_id)
                        for pagebro_line in page_bro:
                            project_page.get_facebook_task(cr,uid,[pagebro_line.id])
                            
                            
                            
        return True


# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
